﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Http;
using System.Net.Http.Headers;

namespace BlogService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private HttpClient client = new HttpClient();
        
        public Service1()
        {
            client.BaseAddress = new Uri("http://localhost:5845/");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
        }

        //CRUD POST
        [WebMethod]
        public string adicionarPost(string conteudo, string autor, string categoria)
        {
            var post = new Post() { DateHora = DateTime.Now, Autor = autor, Categoria = categoria, Conteudo = conteudo };
            
            HttpResponseMessage response = client.PostAsJsonAsync("api/posts", post).Result;

            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Post cadastrado com sucesso!";
            }
            else
            {
                result = response.StatusCode +" - "+ response.ReasonPhrase;
            }

            return result;
        }

        [WebMethod]
        public List<Post> retornarTodosPosts()
        {
            // List all products.
            HttpResponseMessage response = client.GetAsync("api/posts").Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsAsync<List<Post>>().Result;
            }
            else
            {
                return new List<Post>();
            }
        }

        [WebMethod]
        public String atualizarPost(int id, string conteudo, string autor, string categoria)
        {
            var post = new Post() { DateHora = DateTime.Now, Autor = autor, Categoria = categoria, Conteudo = conteudo };

            HttpResponseMessage response = client.PutAsJsonAsync("api/posts/"+id, post).Result;

            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Post atualizado com sucesso!";
            }
            else
            {
                result = response.StatusCode + " - " + response.ReasonPhrase;
            }

            return result;
        }

        [WebMethod]
        public String removerPost(int id)
        {
            HttpResponseMessage response = client.DeleteAsync("api/posts/"+id).Result;
         
            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Post deletado com sucesso!";
            }
            else
            {
                result = response.StatusCode + " - " + response.ReasonPhrase;
            }

            return result;
        }
        
        [WebMethod]
        public Boolean contemPostId(int id)
        {
            // Get a product by ID
            HttpResponseMessage response = client.GetAsync("api/posts/"+id).Result;
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var post = response.Content.ReadAsAsync<Post>().Result;
                if (post != null)
                    return true;
            }

            return false;
        }
        
        /*
         * CRUD COMENTARIO
         */
        
        [WebMethod]
        public string adicionarComentario(int idPost, string conteudo, string autor)
        {
            var comentario = new Comentario() { DateHora = DateTime.Now, Autor = autor, Conteudo = conteudo, IdPost = idPost };

            HttpResponseMessage response = client.PostAsJsonAsync("api/comentarios", comentario).Result;

            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Comentario cadastrado com sucesso!";
            }
            else
            {
                result = response.StatusCode + " - " + response.ReasonPhrase;
            }

            return result;
        }

        [WebMethod]
        public List<Comentario> retornarTodosComentarios(int idPost)
        {
            HttpResponseMessage response = client.GetAsync("api/comentarios?idpost="+idPost).Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsAsync<List<Comentario>>().Result;
            }
            else
            {
                return new List<Comentario>();
            }            
        }

        [WebMethod]
        public String atualizarComentario(int id, string conteudo, string autor, int idPost)
        {
            var comentario = new Comentario() { DateHora = DateTime.Now, Autor = autor, Conteudo = conteudo, IdPost = idPost };

            HttpResponseMessage response = client.PutAsJsonAsync("api/comentarios/" + id, comentario).Result;

            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Comentario atualizado com sucesso!";
            }
            else
            {
                result = response.StatusCode + " - " + response.ReasonPhrase;
            }

            return result; 
        }

        [WebMethod]
        public String removerComentario(int id)
        {
            HttpResponseMessage response = client.DeleteAsync("api/comentarios/" + id).Result;

            string result;

            if (response.IsSuccessStatusCode)
            {
                result = "Comentario deletado com sucesso!";
            }
            else
            {
                result = response.StatusCode + " - " + response.ReasonPhrase;
            }

            return result;   
        }

        [WebMethod]
        public Boolean contemComentarioId(int id)
        {
            // Get a product by ID
            HttpResponseMessage response = client.GetAsync("api/comentarios/" + id).Result;
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var comentario = response.Content.ReadAsAsync<Comentario>().Result;
                if (comentario != null)
                    return true;
            }

            return false;
        }
    }
}